<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 站点全局配置 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\model;


use think\Cache;
use think\Db;

class EnterpriseSiteConfig {

    /**
     * 获取缓存key对象
     *
     * @param $eid
     * @return CacheKey
     */
    private static function _getCacheKey($eid) {
        return CacheKey::module("core")->type("EnterpriseSiteConfig")->tags([
            'eid' => $eid
        ]);
    }

    /**
     * 获取配置
     *
     * @param $eid
     * @param string $name
     * @return array|bool|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function get($eid, $name = "") {
        $enterpriseSiteConfigCacheKey = self::_getCacheKey($eid);
        $enterpriseSiteConfigs = $enterpriseSiteConfigCacheKey->getValue();
        if (!$enterpriseSiteConfigs) {
            $enterpriseSiteConfigs = self::reload($eid);
        }
        if (empty($name)) {
            // 获取全部配置
            $siteConfigs = SiteConfig::get();
            return array_merge($siteConfigs, $enterpriseSiteConfigs);
        } else {
            if (isset($enterpriseSiteConfigs[$name])) {
                return $enterpriseSiteConfigs[$name];
            } else {
                return SiteConfig::get($name);
            }
        }
    }

    /**
     * 更新配置
     *
     * @param $eid
     * @param $name
     * @param $value
     * @param bool $reload
     * @return int|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function set($eid, $name, $value, $reload = true) {
        if (!isset(SiteConfig::$cfs[$name])) {
            // 设置项不在白名单之内
            return SiteConfig::ERR_UNKNOWN_CONFIG_NAME;
        }
        $cf = SiteConfig::$cfs[$name];
        if (isset($cf['options']) && is_array($cf['options']) && !array_key_exists($value, $cf['options'])) {
            // 设置的值错误
            return SiteConfig::ERR_UNKNOWN_CONFIG_VALUE;
        }
        $row = Db::name("EnterpriseSiteConfig")->where([
            'enterprise_id' => $eid,
            'name' => $name
        ])->find();

        $global = Db::name('SiteConfig')->where([
            'name' => $name,
            'value' => $value
        ])->find();

        if (empty($row)) {
            if (empty($global)) {
                $ret = Db::name("EnterpriseSiteConfig")->insert([
                    'enterprise_id' => $eid,
                    'name' => $name,
                    'value' => $value
                ]);
            } else {
                return 1; // 系统默认设置
            }
        } else {
            if ($row['name'] == $global['name']) {
                $ret = Db::name("EnterpriseSiteConfig")->where([
                    'enterprise_id' => $eid,
                    'name' => $name
                ])->delete();   
            } else {
                $ret = Db::name("EnterpriseSiteConfig")->where([
                    'enterprise_id' => $eid,
                    'name' => $name
                ])->update([
                    'value' => $value
                ]);
            }
        }
        if ($reload) {
            self::reload($eid);
        }
        return $global;
    }

    /**
     * 重新加载配置
     *
     * @param $eid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function reload($eid) {
        $rows = Db::name("EnterpriseSiteConfig")->where([
            'enterprise_id' => $eid
        ])->select();
        $configs = [];
        if ($rows) {
            foreach ($rows as $row) {
                $configs[$row['name']] = $row['value'];
            }
        }
        $cacheKey = self::_getCacheKey($eid);
        $cacheKey->setValue($configs);
        return $configs;
    }
}